#!/bin/bash
# Output the postscript file as PNM.
# For some reason, ghostscript can't just read the bounding box and output
# an image that size. So we have to go the long way, determine the bounding
# box, then re-create that with the size and offset determined.
##

if [ $# -ne 2 ] ; then
   echo "usage: $0 <rsolution-dpi> <filename>"
   exit 1
fi

DPI=$1
IN_PS=$2

GS="gs -q -dNOPAUSE -dBATCH"

# Extract offset and size. Offset we need to manually shift the content to the
# origin (as gs does not seem to have an option for it). The size we need
# to know exactly how many pixels need to be rendered.
# Extracting both from the bounding-box run, returned as a "<RES>:<translate>"
# tuple.
#
# Adding some border (3 point offset hence 6 point size), as the
# pixel-size is rounded numbers, gs fails to generate negative offsets in
# the bbox output and we want some space to reliably pnmcrop.
EXTRACT=$($GS -sDEVICE=bbox -sOutputFile=- $IN_PS 2>&1 | \
	   awk "/%HiResBoundingBox/ {
                printf(\"%dx%d:%.1f %.1f translate\",
                       $DPI * (\$4 - \$2 + 6) / 72, $DPI * (\$5 - \$3 + 6) / 72,
                       -(\$2 - 3), -(\$3 - 3)); }")

RES=$(echo "$EXTRACT" | awk -F: '{print $1}')
TRANSLATE=$(echo "$EXTRACT" | awk -F: '{print $2}')

# Now the actual output, cropping to the actual thing.
(echo "$TRANSLATE" ; cat "$IN_PS" ) | $GS -sDEVICE=pnm -sOutputFile=- -r${DPI}x${DPI} -g$RES - | pnmcrop
